package com.yaorange.web;

import com.github.pagehelper.PageInfo;
import com.yaorange.entity.*;
import com.yaorange.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/business/valid")
@CrossOrigin
public class BusinessValidController {


    @Autowired
    private CustomerService customerService;
    @Autowired
    private EmpService empService;
    @Autowired
    private CustomerSourceService customerSourceService;
    @Autowired
    private CarService carService;
    @Resource
    private HouseService houseService;


    //获取所有数据的分页显示
    @GetMapping("/page/{pageNum}/{pageSize}/{account}")
    public PageInfo<Customer> page(@PathVariable Integer pageNum, @PathVariable Integer pageSize,
                                   @PathVariable String account){
        //通过账户获取员工
        Emp emp = empService.getEmpByUserName(account);
        if(emp == null){
            return null;
        }
        return customerService.getValidPage(pageNum,pageSize,emp.getEmpId());
    }

    //获取已签单客户数据分页显示
    @GetMapping("/signedPage/{pageNum}/{pageSize}/{account}")
    public PageInfo<Customer> signedPage(@PathVariable Integer pageNum, @PathVariable Integer pageSize,
                                   @PathVariable String account){
        //通过账户获取员工
        Emp emp = empService.getEmpByUserName(account);
        if(emp == null){
            return null;
        }
        return customerService.getSigendPage(pageNum,pageSize,emp.getEmpId());
    }


    //获取所有客户来源数据
    @GetMapping("/sources")
    public List<CustomerSource> getSourceList(){
        return customerSourceService.getList();
    }


    //回显客户
    @GetMapping("/{id}")
    public Customer customer(@PathVariable Integer id){
        return customerService.getCustomerAndPropertyById(id);
    }


    //回显已签单客户
    @GetMapping("/signed/{id}")
    public Customer customerSigned(@PathVariable Integer id){
        return customerService.getSignedCustomerById(id);
    }



    //新增车产行
    @GetMapping("/car/{customerId}")
    public Car getAddCar(@PathVariable Integer customerId){
        //创建新的车辆对象
        Car car = new Car();
        car.setCustomerId(customerId);//关联客户
        //新增客户
        int rows = carService.addCar(car);
        if(rows > 0){
            return car;
        }
        return null;
    }

    //新增房产行
    @GetMapping("/house/{customerId}")
    public House getAddHouse(@PathVariable Integer customerId){
        //创建新的车辆对象
        House house = new House();
        house.setCustomerId(customerId);//关联客户
        //新增客户
        int rows = houseService.addHouse(house);
        if(rows > 0){
            return house;
        }
        return null;
    }



    //“有效客户”的新增客户按钮
    @PostMapping("/{account}")
    public Map<String,Object> businessAdd(@RequestBody Customer customer,@PathVariable String account){
        Map<String, Object> map = new HashMap<>();
        Emp emp = empService.getEmpByUserName(account);
        if(emp == null){
            map.put("msg","账号异常，请重新登录");
            return map;
        }
        //设置跟踪字段数据
        //设置客户等级
        customer.setCustomerLevel("优");
        //设置关注等级
        customer.setAttentionLevel("A");
        //设置客户可见性为自留(避免自动释放到公共池)
        customer.setVisibility(0);
        //设置客户录入客户类型
        customer.setAddType(2);//表示业务员录入
        //设置录入人员id
        customer.setAddPersonId(emp.getEmpId());
        //设置录入人员名称
        customer.setAddPersonName(emp.getEmpName());

        int rows = customerService.add(customer);

        if(rows > 0){
            map.put("status",20000);
            map.put("msg","客户新增成功");
        }else{
            map.put("msg","客户新增失败");
        }
        return map;
    }


    //客户编辑功能（没有用这个方法，用的另一个web的方法）
    @PutMapping
    public Map<String,Object> update(@RequestBody Customer customer){
        Map<String, Object> map = new HashMap<>();
        //设置新订单客户更新时的默认字段值
        //设置客户状态为跟进中
        customer.setState(3);
        //设置客户通话次数+1
        customer.setCallCount(customer.getCallCount()+1);
        //设置客户isFollow为0，表示至少跟进一次
        customer.setIsFollow(0);
        //设置跟进时间:因为前端传入的对象（没有显示的new对象）没有设置时间，所有跟进时间为null，
        // 只有后端直接new的对象，才能获取默认时间
        customer.setFllowDate(new Date());
        int rows = customerService.updateBySelective(customer);
        if(rows > 0){
            map.put("status",200);
            map.put("msg","客户编辑成功");
        }else{
            map.put("msg","客户编辑失败");
        }
        return map;
    }


    //更新车产到数据库
    @PutMapping("/car")
    public Map<String,Object> updateCar(@RequestBody Car car){
        Map<String, Object> map = new HashMap<>();
        int rows = carService.update(car);
        if(rows > 0){
            map.put("status",20000);
            map.put("msg","车辆信息同步成功");
        }else{
            map.put("msg","车辆信息同步成功");
        }
        return map;
    }

    //更新房产到数据库
    @PutMapping("/house")
    public Map<String,Object> updateHouse(@RequestBody House house){
        Map<String, Object> map = new HashMap<>();
        int rows = houseService.update(house);
        if(rows > 0){
            map.put("status",20000);
            map.put("msg","房产信息同步成功");
        }else{
            map.put("msg","房产信息同步成功");
        }
        return map;
    }





    //有效客户 签单功能
    @PutMapping("/ordered/{account}")
    public Map<String,Object> ordered(@RequestBody  Customer customer , @PathVariable String account){
        Map<String, Object> map = new HashMap<>();
        Emp emp = empService.getEmpByUserName(account);
        customer.setState(4);
        customer.setEmpId(emp.getEmpId());
        customer.setDeptId(emp.getDeptId());
        customer.setCreateDate(new Date());
        int rows = customerService.updateCustomerWithOrder(customer);
        if(rows > 0){
            map.put("status",200);
            map.put("msg","客户签单成功");
        }else{
            map.put("msg","客户签单失败");
        }
        return map;
    }




    //车产的删除
    @DeleteMapping("/car/{carId}")
    public Map<String,Object> deleteCar(@PathVariable Integer carId){
        Map<String, Object> map = new HashMap<>();
        int rows = carService.delete(carId);
        if(rows > 0){
            map.put("status",20000);
            map.put("msg","车辆删除成功");
        }else{
            map.put("msg","车辆删除失败");
        }
        return map;
    }

    //车产的删除
    @DeleteMapping("/house/{houseId}")
    public Map<String,Object> deleteHouse(@PathVariable Integer houseId){
        Map<String, Object> map = new HashMap<>();
        int rows = houseService.delete(houseId);
        if(rows > 0){
            map.put("status",20000);
            map.put("msg","房产删除成功");
        }else{
            map.put("msg","房产删除失败");
        }
        return map;
    }





}
